<?php
declare(strict_types=1);

namespace Zms\Unit;

use DateTime;

class Time
{

    /**
     * 获取日期时间值,根据传入的值返回10位时间戳
     * @param string|int $time
     * @return int
     */
    public static function value($time): int
    {
        $time = strval($time);
        if (preg_match('/^(\d{10})$/', $time)) {
            return intval($time);
        } elseif (preg_match('/^(\d{13})$/', $time)) {
            return intval(intval($time) / 1000);
        }
        return strtotime($time);
    }


    /**
     * 获取毫秒时间戳
     * @return int
     */
    public static function microtime(): int
    {
        $time = microtime(true);
        return (int)round($time * 1000);
    }


    /**
     * 获取今天的开始和结束日期
     * @return array{start:string,start_timestamp:int,end:string,end_timestamp:int}
     * @throws \DateMalformedStringException
     */
    public static function getTodayRange(): array
    {
        $startOfDay = (new DateTime('today'))->format('Y-m-d H:i:s');
        $endOfDay = (new DateTime('tomorrow'))->modify('-1 second')->format('Y-m-d H:i:s');
        return [
            'start' => $startOfDay,
            'start_timestamp' => strtotime($startOfDay),
            'end' => $endOfDay,
            'end_timestamp' => strtotime($endOfDay)
        ];
    }


    /**
     * 获取昨天的开始和结束日期
     * @return array{start:string,start_timestamp:int,end:string,end_timestamp:int}
     * @throws \DateMalformedStringException
     */
    public static function getYesterdayRange(): array
    {
        // 昨天的起始时间（昨天午夜 00:00:00）
        $startOfYesterday = (new DateTime('yesterday'))->format('Y-m-d H:i:s');
        // 昨天的结束时间（昨天的最后一秒 23:59:59）
        $endOfYesterday = (new DateTime('today'))->modify('-1 second')->format('Y-m-d H:i:s');
        return [
            'start' => $startOfYesterday,
            'start_timestamp' => strtotime($startOfYesterday),
            'end' => $endOfYesterday,
            'end_timestamp' => strtotime($endOfYesterday)
        ];
    }


    /**
     * @describe 获取本周的时间范围
     * @user zlf <zms-pro@qq.com> 2025/4/18
     * @return array{start:string,start_timestamp:int,end:string,end_timestamp:int}
     * @throws \DateMalformedStringException
     */
    public static function getCurrentWeekRange(): array
    {
        // 创建一个 DateTime 对象，设置为当前日期时间
        $now = new DateTime();
        // 计算本周开始的时间（即本周一的午夜 00:00:00）
        $startOfWeek = (clone $now)->modify('Monday this week')->format('Y-m-d H:i:s');
        // 计算本周结束的时间（即本周日的最后一秒 23:59:59）
        $endOfWeek = (clone $now)->modify('Sunday this week')->modify('+1 day -1 second')->format('Y-m-d H:i:s');
        return [
            'start' => $startOfWeek,
            'start_timestamp' => strtotime($startOfWeek),
            'end' => $endOfWeek,
            'end_timestamp' => strtotime($endOfWeek)
        ];
    }

    /**
     * @describe 获取本月的时间范围
     * @user zlf <zms-pro@qq.com> 2025/4/18
     * @return array{start:string,start_timestamp:int,end:string,end_timestamp:int}
     */
    public static function getCurrentMonthRange(): array
    {
        $month = date("Y-m");
        $startOfMonth = "{$month}-01 00:00:00";
        $endOfMonth = "{$month}-" . self::getCurrentMonthDayNum() . " 23:59:59";
        return [
            'start' => $startOfMonth,
            'start_timestamp' => strtotime($startOfMonth),
            'end' => $endOfMonth,
            'end_timestamp' => strtotime($endOfMonth)
        ];
    }


    /**
     * @describe 获取本月天数
     * @user zlf <zms-pro@qq.com> 2025/4/18
     * @return int
     */
    public static function getCurrentMonthDayNum(): int
    {
        return (int)(new DateTime())->format('t');
    }
}